<?php

require_once('includes/config.php');
require_once('libraries/database/impl/SchemaManagerImpl.php');

require_once('PHPUnit/Framework/TestCase.php');

/**
 * SchemaManagerImpl test case.
 */
class SchemaManagerImplTest extends PHPUnit_Framework_TestCase {
	
	private $SchemaManagerImpl;

	protected function setUp() {
		parent::setUp ();
		$this->SchemaManagerImpl = new SchemaManagerImpl();
	}
	
	protected function tearDown() {
		$this->SchemaManagerImpl = null;
		parent::tearDown ();
	}

	public function testSetSchemaFile() {
		$this->SchemaManagerImpl->setSchema(TEST_LIBRARIES . 'database/schemaTest.xml', true);
		$this->assertContains("database", $this->SchemaManagerImpl->getData(), 'Database not found.');
	}
	
	public function testSetSchemaData() {
		$data = "<database name=\"test\"><table name=\"testTable\"><field name=\"field\" type=\"varchar\" size=\"255\" null=\"no\" /></table></database>";
		$this->SchemaManagerImpl->setSchema($data, false);
		$this->assertContains("database", $this->SchemaManagerImpl->getData(), 'Database not found');
	}
	
	public function testParseSchema() {
		$this->SchemaManagerImpl->setSchema(TEST_LIBRARIES . 'database/schemaTest.xml', true);
		$this->SchemaManagerImpl->parseSchema();
		
		$this->assertEquals(5, count($this->SchemaManagerImpl->getSql()), 'Incorrect number of SQL statements');
		
		foreach($this->SchemaManagerImpl->getSql() AS $key => $data) {
			if (strpos($data, 'DROP D') !== false) { // DROP DATABASE
				$this->assertContains('DROP DATABASE IF EXISTS testDatabase', $data, 'Unable to find DROP DATABASE statement');
			} elseif (strpos($data, 'CREATE D') !== false) { // CREATE DATABASE
				$this->assertContains('IF NOT EXISTS testDatabase', $data, 'Unable to find database name in CREATE DATABASE statement.');
			} elseif (strripos($data, 'USE') !== false) { // USE statement
				$this->assertContains('test', $data, 'Unable to find database name in USE statement.');
			} elseif (strripos($data, 'DROP T') !== false) { // Drop table statement
				$this->assertEquals('DROP TABLE IF EXISTS testTable', $data, 'DROP TABLE statement is bad.');
			} elseif (strripos($data, 'CREATE T') !== false) {
				$this->assertContains('CREATE TABLE IF NOT EXISTS testTable', $data, 'Unable to find table creation.');
				$this->assertcontains('id int(11) NOT NULL AUTO_INCREMENT', $data, 'Unable to find ID field.');
				$this->assertcontains('name varchar(50) NOT NULL', $data, 'Unable to find NAME field.');
				$this->assertcontains('value text NULL', $data, 'Unable to find VALUE field.');
			} else {
				$this->fail('Something broke.');
			}
		}
	}
	
	public function testCreateDatabase() {
		$this->SchemaManagerImpl->setSchema(TEST_LIBRARIES . 'database/schemaTest.xml', true);
		$this->SchemaManagerImpl->parseSchema();
		$this->SchemaManagerImpl->createDatabase(NULL, TEST_DB_HOST, TEST_DB_USER, TEST_DB_PASS, TEST_DB_DATABASE);
		
		$this->assertEquals(0, count($this->SchemaManagerImpl->getSqlErrors()), 'Database failed to create.');
	}
	
	private function strstrb($haystack, $needle) {
		return array_shift(explode($needle, $haystack, 2));
	}
}

